package com.yhp.servlet;

import com.yhp.bean.Student;
import com.yhp.service.impl.StudentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/Educational/student/updatastu")
public class UpDataStudentServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取数据
        String stuid = req.getParameter("stuid");
        String stuno = req.getParameter("stuno");
        String stuname = req.getParameter("stuname");
        String gid = req.getParameter("gid");
        String sex = req.getParameter("sex");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String registered = req.getParameter("registered");
        String address = req.getParameter("address");
        String politics = req.getParameter("politics");
        String idnumber = req.getParameter("idnumber");
        String profession = req.getParameter("profession");
        //2.调取service，更新数据
        Student student = new Student();
        student.setStuid(Integer.parseInt(stuid));
        student.setStuno(stuno);
        student.setStuname(stuname);
        student.setGid(Integer.parseInt(gid));
        student.setSex(Integer.parseInt(sex));
        student.setEmail(email);
        student.setPhone(Long.parseLong(phone));
        student.setRegistered(registered);
        student.setAddress(address);
        student.setPolitics(politics);
        student.setIdnumber(Long.parseLong(idnumber));
        student.setProfession(profession);
        student.setIntroduction(req.getParameter("introduction"));
        int zt = StudentService.upDate(student);
        //3.跳转页面，反馈修改结果
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (zt>0){
            writer.println("<script>alert('修改成功！');location.href='/Educational/student/getStudentList';</script>");
        }else {
            writer.println("<script>alert('修改失败！');location.href='/Educational/student/findbyid？sid="+stuid+"';</script>");
        }
    }
}
